using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;
using System.Windows.Forms.VisualStyles;
using IReaper.FileData;

namespace IReaper.CourseData
{
    public class DataGridViewDownloadTaskCell : DataGridViewImageCell
    {
        #region static variable and constructor
        static Bitmap pptImage, codeImage, videoImage, qaImage, mp3Image, mp4Image, zuneImage;
        static Bitmap pptLightImage, codeLightImage, videoLightImage,qaLightImage,mp3LightImage, mp4LightImage, zuneLightImage;
        static Bitmap completeImage, emptyImage;
        static Graphics g;

        static DataGridViewDownloadTaskCell()
        {
            //load image
            //orignial
            int length = 0;
            length += 10;

            Image image = (Image)Properties.Resources.ResourceManager.GetObject("PPT_Small");
            pptImage = (Bitmap)image;
            pptImage.MakeTransparent(Color.White);
            length += 20;
            length += 10;
            codeImage = (Bitmap)Properties.Resources.ResourceManager.GetObject("Code_Small");
            codeImage.MakeTransparent(Color.White);
            length += 20;
            length += 10;
            videoImage = (Bitmap)Properties.Resources.ResourceManager.GetObject("Video_Small");
            videoImage.MakeTransparent(Color.White);
            length += 20;
            length += 10;
            qaImage = (Bitmap)Properties.Resources.qa.GetThumbnailImage(20, 20, null, IntPtr.Zero);
            qaImage.MakeTransparent(Color.White);
            length += 20;
            length += 10;
            mp3Image = Properties.Resources.mp3;
            mp3Image.MakeTransparent(Color.White);
            length += 20;
            length += 10;
            mp4Image = Properties.Resources.mp4;
            mp4Image.MakeTransparent(Color.White);
            length += 20;
            length += 10;
            zuneImage = Properties.Resources.Zune;
            zuneImage.MakeTransparent(Color.White);
            length += 20;
            length += 10;
            //light image
            pptLightImage = (Bitmap)Properties.Resources.ResourceManager.GetObject("PPT_Light");
            pptLightImage.MakeTransparent(Color.White);
            codeLightImage = (Bitmap)Properties.Resources.ResourceManager.GetObject("Code_Light");
            codeLightImage.MakeTransparent(Color.White);
            videoLightImage = (Bitmap)Properties.Resources.ResourceManager.GetObject("Video_Light2");
            videoLightImage.MakeTransparent(Color.White);

            qaLightImage = (Bitmap)Properties.Resources.lightqa.GetThumbnailImage(20, 20, null, IntPtr.Zero);
            qaLightImage.MakeTransparent(Color.White);
            mp3LightImage = Properties.Resources.mp3_Gray;
            mp3LightImage.MakeTransparent(Color.White);
            mp4LightImage = Properties.Resources.mp4_Gray;
            mp4LightImage.MakeTransparent(Color.White);
            zuneLightImage = Properties.Resources.Zune_Gray;
            zuneLightImage.MakeTransparent(Color.White);
            //create an empty image
            completeImage = new Bitmap(length, 20, System.Drawing.Imaging.PixelFormat.Format64bppPArgb);
            emptyImage = new Bitmap(length, 20, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
            emptyImage.MakeTransparent(Color.Black);
            //get graphics object from completeImage
            g = Graphics.FromImage(completeImage);
        }
        #endregion

        protected override object GetFormattedValue(object value,
                            int rowIndex, ref DataGridViewCellStyle cellStyle,
                            TypeConverter valueTypeConverter,
                            TypeConverter formattedValueTypeConverter,
                            DataGridViewDataErrorContexts context)
        {
                if (this.DataGridView.Rows.Count < rowIndex)
                { return emptyImage; }
                Course course = null;
                try
                {
                    course = this.DataGridView.Rows[rowIndex].DataBoundItem as Course;
                }
                catch
                {
                    return emptyImage;
                }
                if (course == null)
                {
                    return emptyImage;
                }
                g.Clear(Color.Transparent);
                //draw reminder
                if (!Course.IsNewCourse(course))
                {
                    DrawOldCourse(course);
                }

                return completeImage;
        }

        private void DrawOldCourse(Course course)
        {
            //get thre course
            CourseFileData codeData = course.Code;
            CourseFileData vidoeData = course.Video;
            CourseFileData pptData = course.PPT;
            CourseFileData qaData = course.QA;
            CourseFileData mp3Data = course.MP3;
            CourseFileData mp4Data = course.MP4;
            CourseFileData wmvData = course.WMV;
            
            int offset = 10;
            //draw propimage;
            //draw ppt
            if (pptData == null)
            {
                g.DrawImage(emptyImage, offset, 0);
            }
            else if (pptData.LifetimeStatue == LifetimePosition.DownloadProcessed)
                g.DrawImage(pptImage, offset, 0);
            else
                g.DrawImage(pptLightImage, offset, 0);
            //draw video
            offset += 30;
            if (vidoeData == null)
                g.DrawImage(emptyImage, offset, 0);
            else if (vidoeData.LifetimeStatue == LifetimePosition.DownloadProcessed)
                g.DrawImage(videoImage, offset, 0);
            else
                g.DrawImage(videoLightImage, offset, 0);
            // draw zune
            offset += 30;
            if (wmvData == null)
                g.DrawImage(emptyImage, offset, 0);
            else if (wmvData.LifetimeStatue == LifetimePosition.DownloadProcessed)
                g.DrawImage(zuneImage, offset, 0);
            else
                g.DrawImage(zuneLightImage, offset, 0);
            // draw mp3
            offset += 30;
            if (mp3Data == null)
                g.DrawImage(emptyImage, offset, 0);
            else if (mp3Data.LifetimeStatue == LifetimePosition.DownloadProcessed)
                g.DrawImage(mp3Image, offset, 0);
            else
                g.DrawImage(mp3LightImage, offset, 0);

            // draw mp4
            offset += 30;
            if (mp4Data == null)
                g.DrawImage(emptyImage, offset, 0);
            else if (mp4Data.LifetimeStatue == LifetimePosition.DownloadProcessed)
                g.DrawImage(mp4Image, offset, 0);
            else
                g.DrawImage(mp4LightImage, offset, 0);
            //draw code
            offset += 30;
            if (codeData == null)
                g.DrawImage(emptyImage, offset, 0);
            else if (codeData.LifetimeStatue == LifetimePosition.DownloadProcessed)
                g.DrawImage(codeImage, offset, 0);
            else
                g.DrawImage(codeLightImage, offset, 0);
            //draw qa
            offset += 30;
            if (qaData == null)
                g.DrawImage(emptyImage, offset, 0);
            else if (qaData.LifetimeStatue == LifetimePosition.DownloadProcessed)
                g.DrawImage(qaImage, offset, 0);
            else
                g.DrawImage(qaLightImage, offset, 0);
                
        }

       
    }
}